package com.mrjy.share.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.entity.ShareCodeInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.share.bo.IShareCodeBo;

@Controller
@RequestMapping("/share/shareCode")
public class ShareCodeContorller{
	
	@Resource
	private IShareCodeBo shareCodeBo;
	/**
	 * 批量插入分享码
	 * @param session
	 * @author Javin
	 * @return
	 */
	@RequestMapping("/insertShareCode")
	@ResponseBody
	public Object insertShareCode(HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return shareCodeBo.txInsert(userInfo);
	}
	
	/**
	 * 查询分享码
	 * @param session
	 * @return
	 */
	@RequestMapping("/queryShareCodeInfo")
	@ResponseBody
	public Object queryShareCodeInfo(HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		return shareCodeBo.queryShareCodeInfo(userInfo);
	}
	/**
	 * 检查是否被一级分销员邀请
	 * @param session
	 * @param userId
	 * @return
	 */
	@RequestMapping("/isInvited")
	@ResponseBody
	public Object isInvited(HttpSession session,@RequestParam("mobilePhone")String mobilePhone){
		Map<String , Object> map = new HashMap<String, Object>();
		//UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		shareCodeBo.isInvited(mobilePhone , map );
		return map;
	}
	/**
	 * 更新被邀请的状态
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateInvitedStatus")
	@ResponseBody
	public Object updateInvitedStatus(HttpSession session,ShareCodeInfo info){
		Map<String , Object> map = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		//获取当前用户的id，点同意的时候用到
		info.setUserId(userInfo.getId());
		shareCodeBo.txInvitedStatus(info , map );
		return map;
	}
	
	/**
	 * 统计一级分销下所有二级分销成员及其推荐总人数
	 * @param info
	 * @param session
	 * @author Javin
	 * @return
	 */
	@RequestMapping("/queryCountSecondary")
	@ResponseBody
	public Object queryCountSecondary(ShareCodeInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getUserId() ==null){
			info.setUserId(userInfo.getId());
		}
		
		return shareCodeBo.queryCountSecondary(info);
	}
	/**
	 * 统计一级分销下所有二级分销成员分销总数
	 * @param info
	 * @param session
	 * @author Javin
	 * @return
	 */
	@RequestMapping("countAllByUserId")
	@ResponseBody
	public Object countAllByUserId(ShareCodeInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getUserId() ==null){
			info.setUserId(userInfo.getId());
		}
		return shareCodeBo.countAllByUserId(info);
	}
	
	/**
	 * 查询未推荐的推荐码及已推荐的二级成员详情
	 * @param info
	 * @param session
	 * @author Javin
	 * @return
	 */
	@RequestMapping("/queryShareCodeDetail")
	@ResponseBody
	public Object queryShareCodeDetail(ShareCodeInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getUserId() ==null){
			info.setUserId(userInfo.getId());
		}
		return shareCodeBo.queryShareCodeDetail(info);
	}
	/**
	 * 查询二级分销员的详细信息，二级分销员主页信息
	 * @param info
	 * @param session
	 * @return
	 */
	@RequestMapping("/querySecondDetailInfo")
	@ResponseBody
	public Object querySecondDetailInfo(ShareCodeInfo info,HttpSession session){
		Map<String , Object> map = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getMobilePhone() ==null){
			info.setMobilePhone(userInfo.getMobilePhone());
		}
		shareCodeBo.querySecondDetailInfo(info , map);
		return map;
	}
	/**
	 * 通过一级分销员的userId获取子分享码
	 * @param info
	 * @param session
	 * @return
	 */
	/*@RequestMapping("/querySubShareCodeByUserId")
	@ResponseBody
	public Object querySubShareCodeByUserId(ShareCodeInfo info,HttpSession session){
		Map<String , Object> map = new HashMap<String, Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getUserId() ==null){
			info.setUserId(userInfo.getId());
		}
		shareCodeBo.querySubShareCodeByUserId(info , map);
		return map;
	}*/
	
	@RequestMapping("/sendInvited")
	@ResponseBody
	public Object sendInvited(ShareCodeInfo info,HttpSession session){
		return shareCodeBo.txSendInvited(info);
	}
	
	/**
	 * 查询一级未分销的的二级子码,(插入二级子码钱查询)limit 1
	 * @param info
	 * @param session
	 * @author Javin
	 * @return
	 */
	@RequestMapping("/queryUnshareCode")
	@ResponseBody
	public Object queryUnshareCode(ShareCodeInfo info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(info.getUserId() == null){
			info.setUserId(userInfo.getId());
		}
		return shareCodeBo.queryUnshareCode(info);
	}
	
}
